package com.twotiger.and.activity.user;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.os.SystemClock;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import app.twotiger.p2p.R;
import cn.jpush.android.api.JPushInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.CheckVcode;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.bean.RegistVoiceBean;
import com.twotiger.and.bean.Register;
import com.twotiger.and.bean.User;
import com.twotiger.and.util.ActivitySwitcher;
import com.twotiger.and.util.ActivitySwitcher.AnimationFinishedListener;
import com.twotiger.and.util.PackageUtils;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ReChargeClickListener;
import com.twotiger.and.util.RegexTool;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.Tools;
import com.twotiger.and.util.ViewUtils;

public class LoginPage extends BaseActivity implements OnClickListener {

    private static final String TAG = "HomeFragment";
    private static final int LOGINSUCCES = 0;
    private static final int LOGINFAIL = 1;

    private static final int SENDVCODESUCCES = 2;
    private static final int SENDVCODEFAIL = 3;
    private static final int TIMECOUNTMESSAGE = 4;
    private static final int CHECKVCODESUSSES = 5;
    private static final int CHECKVCODEFAIL = 6;
    protected static final int DISMISSDIAOLOG = 7;
    private static final int REGISTSUCCES = 8;
    private static final int REGISTFAIL = 9;
    private static final int SENDVCODEMSGSUCCES = 10;
    private static final int SENDVCODEMSGFAIL = 11;
    private static final int CHECKVCODEMSGSUCCES = 12;
    private static final int CHECKVCODEMSGFAIL = 13;
    private static final int MODIFYPSWSUCCES = 14;
    private static final int MODIFYPSWFAIL = 15;

    private String registToken;

    // 倒计时用计数
    private int time;
    // 按钮上的文字
    private String btnMsg = "";
    private Timer timer;
    private boolean isShow = false;
    private String findPwdNextToken;

    private Button find_password_bt;
    public EditText login_psd_et;
    public EditText login_mobile_et;
    private Button login_bt;
    private HashMap<String, String> params;
    private Button rigst_bt;
    private SubTitle subTitle;
    private ImageView iv_finish, iv_finish2, iv_finish3, iv_finish4;
    private View login_layout, regist_layout, registnext_layout, findpwd_layout,
            findpwdnext_layout;
    private Button regist_button;
    private Button regist_verification_bt;
    private TextView tv_backtologin, tv_backtologin2, tv_backtologin3,tv_backtologin4;
    private EditText regist_mobile_et;
    private EditText regist_verification_ev;
    private EditText invitation_edit;
    private TextView getvoice_vcode;
    private String mobile;
    private String verify_type = null;//请求验证码返回的方式

    private AlertDialog alertDialog;
    private CheckBox psw_isvisable;
    private EditText set_login_psw;
    private Button regist_btn;
    private EditText phone;
    private Button code_btn;
    private EditText phone_code;
    private Button next;
    private ImageView iv_finish5;
    private EditText set_psw_et;
    private ImageView psw_isvisable_pwdnext;
    private Button next_btn;
    private String countDownType;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.login_layout, null);
        if ("back".equals(getIntent().getStringExtra("isNeedAnim"))) {
            ActivitySwitcher.animationInBack(view, getWindowManager());
        }
        return view;
    }

    @Override
    protected void findViewById(final View view) {
        //login界面
        iv_finish = (ImageView) view.findViewById(R.id.iv_finish);
        iv_finish2 = (ImageView) view.findViewById(R.id.iv_finish2);
        iv_finish3 = (ImageView) view.findViewById(R.id.iv_finish3);
        find_password_bt = (Button) view.findViewById(R.id.find_password_bt);
        login_bt = (Button) view.findViewById(R.id.login_bt);
        login_psd_et = (EditText) view.findViewById(R.id.login_psd_et);
        login_mobile_et = (EditText) view.findViewById(R.id.login_mobile_et);
        rigst_bt = (Button) view.findViewById(R.id.regist_bt);
        login_layout = view.findViewById(R.id.login_layout);

        //register界面
        regist_button = (Button) view.findViewById(R.id.regist_button);
        regist_verification_bt = (Button) view.findViewById(R.id.regist_verification_bt);
        tv_backtologin = (TextView) view.findViewById(R.id.tv_backtologin);
        tv_backtologin2 = (TextView) view.findViewById(R.id.tv_backtologin2);
        regist_mobile_et = (EditText) view.findViewById(R.id.regist_mobile_et);
        regist_verification_ev = (EditText) view.findViewById(R.id.regist_verification_ev);
        invitation_edit = (EditText) view.findViewById(R.id.invitation_edit);
        getvoice_vcode = (TextView) findViewById(R.id.getvoice_vcode);
        regist_layout = view.findViewById(R.id.regist_layout);

        //registernext界面
        registnext_layout = view.findViewById(R.id.registnext_layout);
        psw_isvisable = (CheckBox) view.findViewById(R.id.psw_isvisable);
        set_login_psw = (EditText) view.findViewById(R.id.set_login_psw);
        regist_btn = (Button) view.findViewById(R.id.regist_btn);

        //findpwd界面

        phone = (EditText) view.findViewById(R.id.phone);
        code_btn = (Button) view.findViewById(R.id.code_btn);
        phone_code = (EditText) view.findViewById(R.id.phone_code);
        next = (Button) view.findViewById(R.id.next);
        iv_finish4 = (ImageView) view.findViewById(R.id.iv_finish4);
        tv_backtologin3 = (TextView) view.findViewById(R.id.tv_backtologin3);
        findpwd_layout = view.findViewById(R.id.findpwd_layout);

        //findpwdnext界面
        iv_finish5 = (ImageView) view.findViewById(R.id.iv_finish5);
        tv_backtologin4 = (TextView) view.findViewById(R.id.tv_backtologin4);
        set_psw_et = (EditText) view.findViewById(R.id.set_psw_et);
        psw_isvisable_pwdnext = (ImageView) view.findViewById(R.id.psw_isvisable_pwdnext);
        next_btn = (Button) view.findViewById(R.id.next_btn);
        findpwdnext_layout = view.findViewById(R.id.findpwdnext_layout);

        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };

    }

    private void modifypsw(String pswstr) {
        params.clear();
        params.put("token", findPwdNextToken);
        params.put("password", pswstr);
        postDate(params, Constants.FORGETPWD, handler, MODIFYPSWSUCCES, MODIFYPSWFAIL, true, false,
            false);
    }

    @Override
    public void onClick(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        }
        switch (view.getId()) {
        case R.id.next_btn:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            String pswstr = set_psw_et.getText().toString().trim();
            ConstantDataUnit zz_mm = getConstantDataByKey(Constants.zz_mm);
            if (zz_mm != null) {
                if (!RegexTool.match(zz_mm.getVal(), pswstr)) {
                    showToast("密码支持8~32位数字和字母");
                    return;
                }
            } else {
                if (!RegexTool.match(RegexTool.REG_PASSWORD, pswstr)) {
                    showToast("密码支持8~32位数字和字母");
                    return;
                }
            }
            modifypsw(pswstr);
            break;

        case R.id.iv_finish5:
        case R.id.iv_finish4:
        case R.id.iv_finish2:
        case R.id.iv_finish3:
        case R.id.iv_finish:
            finish();
            overridePendingTransition(R.anim.silent_anim, R.anim.slide_out_to_bottom);
            break;
        case R.id.find_password_bt:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            ActivitySwitcher.animationOutForward(login_layout, getWindowManager(),
                new ActivitySwitcher.AnimationFinishedListener() {
                    @Override
                    public void onAnimationFinished() {
                        login_layout.setVisibility(View.GONE);
                        findpwd_layout.setVisibility(View.VISIBLE);
                        ActivitySwitcher.animationInForward(findpwd_layout, getWindowManager());
                    }
                });
            break;
        case R.id.regist_bt:
            ActivitySwitcher.animationOutForward(login_layout, getWindowManager(),
                new ActivitySwitcher.AnimationFinishedListener() {
                    @Override
                    public void onAnimationFinished() {
                        login_layout.setVisibility(View.GONE);
                        regist_layout.setVisibility(View.VISIBLE);
                        ActivitySwitcher.animationInForward(regist_layout, getWindowManager());
                    }
                });
            break;
        case R.id.login_bt:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            String psd = login_psd_et.getText().toString().trim();
            mobile = login_mobile_et.getText().toString().trim();
            ConstantDataUnit zz_sjh_lo = getConstantDataByKey(Constants.zz_sjh);
            if (zz_sjh_lo != null) {
                if (StringUtils.isEmpty(mobile) || !RegexTool.match(zz_sjh_lo.getVal(), mobile)) {
                    showToast("请输入有效的手机号");
                    return;
                }
            } else {
                if (TextUtils.isEmpty(mobile) || !RegexTool.regexPhoneNumber(mobile)) { //手机号正则匹配
                    showToast("请输入有效的手机号");
                    return;
                }
            }
            if (StringUtils.isEmpty(psd)) {
                showToast("请输入密码");
                return;
            }
            login(mobile, psd);
            break;

        //注册界面操作
        case R.id.regist_verification_bt: // 获取短信验证码
            mobile = regist_mobile_et.getText().toString().trim();
            if (StringUtils.isEmpty(mobile)) {
                showToast("请输入有效手机号");
                return;
            }
            ConstantDataUnit zz_sjh = getConstantDataByKey(Constants.zz_sjh);
            if (zz_sjh != null) {
                if (!RegexTool.match(zz_sjh.getVal(), mobile)) {
                    showToast("请输入有效手机号");
                    return;
                }
            } else {
                if (!RegexTool.regexPhoneNumber(mobile)) {
                    showToast("请输入有效手机号");
                    return;
                }
            }
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            verify_type = "sms";
            getVcode(mobile, verify_type);
            break;
        case R.id.tv_backtologin4:  //找回密码次页
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            ActivitySwitcher.animationOutBack(findpwdnext_layout, getWindowManager(),
                new ActivitySwitcher.AnimationFinishedListener() {
                    @Override
                    public void onAnimationFinished() {
                        findpwdnext_layout.setVisibility(View.GONE);
                        findpwd_layout.setVisibility(View.VISIBLE);
                        ActivitySwitcher.animationInBack(findpwd_layout, getWindowManager());
                    }
                });
            break;
        case R.id.tv_backtologin3:  //找回密码第一页
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            ActivitySwitcher.animationOutBack(findpwd_layout, getWindowManager(),
                new ActivitySwitcher.AnimationFinishedListener() {
                    @Override
                    public void onAnimationFinished() {
                        application.logout();
                        login_mobile_et.setText(application.getUser().phone);
                        findpwd_layout.setVisibility(View.GONE);
                        login_layout.setVisibility(View.VISIBLE);
                        ActivitySwitcher.animationInBack(login_layout, getWindowManager());
                    }
                });
            break;
        case R.id.tv_backtologin2:   //注册第二页
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            ActivitySwitcher.animationOutBack(registnext_layout, getWindowManager(),
                new ActivitySwitcher.AnimationFinishedListener() {
                    @Override
                    public void onAnimationFinished() {
                        registnext_layout.setVisibility(View.GONE);
                        regist_layout.setVisibility(View.VISIBLE);
                        ActivitySwitcher.animationInBack(regist_layout, getWindowManager());
                    }
                });
            break;
        case R.id.tv_backtologin:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            ActivitySwitcher.animationOutBack(regist_layout, getWindowManager(),
                new ActivitySwitcher.AnimationFinishedListener() {
                    @Override
                    public void onAnimationFinished() {
                        application.logout();
                        login_mobile_et.setText(application.getUser().phone);
                        regist_layout.setVisibility(View.GONE);
                        registnext_layout.setVisibility(View.GONE);
                        login_layout.setVisibility(View.VISIBLE);
                        ActivitySwitcher.animationInBack(login_layout, getWindowManager());
                    }
                });
            break;
        case R.id.getvoice_vcode: //获取语音验证码
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            mobile = regist_mobile_et.getText().toString().trim();
            if (StringUtils.isEmpty(mobile)) {
                showToast("请输入有效手机号");
                return;
            }
            ConstantDataUnit zz_sjh_2 = getConstantDataByKey(Constants.zz_sjh);
            if (zz_sjh_2 != null) {
                if (!RegexTool.match(zz_sjh_2.getVal(), mobile)) {
                    showToast("请输入有效手机号");
                    return;
                }
            } else {
                if (!RegexTool.regexPhoneNumber(mobile)) {
                    showToast("请输入有效手机号");
                    return;
                }
            }
            verify_type = "voice";
            getVcode(mobile, verify_type);

            break;
        case R.id.regist_button:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            String verification_code = regist_verification_ev.getText().toString().trim();
            String mobile = regist_mobile_et.getText().toString().trim();

            ConstantDataUnit zz_sjh_re = getConstantDataByKey(Constants.zz_sjh);
            if (zz_sjh_re != null) {
                if (TextUtils.isEmpty(mobile) || !RegexTool.match(zz_sjh_re.getVal(), mobile)) { //手机号正则匹配
                    showToast("请输入有效的手机号");
                    return;
                }
            } else {
                if (TextUtils.isEmpty(mobile) || !RegexTool.regexPhoneNumber(mobile)) { //手机号正则匹配
                    showToast("请输入有效的手机号");
                    return;
                }
            }
            if (TextUtils.isEmpty(verification_code)) {
                showToast("请输验证码");
                return;
            }
            
            params.clear();
            params.put("phone", mobile);
            params.put("code", verification_code);
            getDate(params, Constants.VERIFYCAP, handler, CHECKVCODESUSSES, CHECKVCODEFAIL, true,
                false, false);
            break;

        //registernext界面操作
        case R.id.regist_btn:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            String psw = set_login_psw.getText().toString().trim();
            ConstantDataUnit zz_mm_next = getConstantDataByKey(Constants.zz_mm);
            if (zz_mm_next != null) {
                if (!RegexTool.match(zz_mm_next.getVal(), psw)) {
                    showToast("密码支持8~32位数字和字母");
                    return;
                }
            } else {
                if (!RegexTool.match(RegexTool.REG_PASSWORD, psw)) {
                    showToast("密码支持8~32位数字和字母");
                    return;
                }
            }
            regist(psw);
            break;

        //findpwd 界面操作

        case R.id.next:

            String phoneStr = phone.getText().toString().trim();
            String codeStr = phone_code.getText().toString().trim();
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            ConstantDataUnit zz_sjh_3 = getConstantDataByKey(Constants.zz_sjh);
            if (zz_sjh_3 != null) {
                if (TextUtils.isEmpty(phoneStr) || !RegexTool.match(zz_sjh_3.getVal(), phoneStr)) { //手机号正则匹配
                    showToast("请输入有效的手机号");
                    return;
                }
            } else {
                if (TextUtils.isEmpty(phoneStr) || !RegexTool.regexPhoneNumber(phoneStr)) { //手机号正则匹配
                    showToast("请输入有效的手机号");
                    return;
                }
            }
            if (TextUtils.isEmpty(codeStr)) {
                showToast("请输验证码");
                return;
            }
            //进行验证码的校验
            checkVcode(phoneStr, codeStr);

            break;
        case R.id.code_btn:

            String phoneStr2 = phone.getText().toString().trim();
            String codeStr2 = phone_code.getText().toString().trim();
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (TextUtils.isEmpty(phoneStr2)) {
                showToast("请输入有效手机号");
                return;
            }
            ConstantDataUnit zz_sjh_1 = getConstantDataByKey(Constants.zz_sjh);
            if (zz_sjh_1 != null) {
                if (!RegexTool.match(zz_sjh_1.getVal(), phoneStr2)) {
                    showToast("请输入有效手机号");
                    return;
                }
            } else {
                if (!RegexTool.regexPhoneNumber(phoneStr2)) {
                    showToast("请输入有效手机号");
                    return;
                }
            }
            getauthcode(phoneStr2);
            break;
        default:
            break;
        }
    }

    private void getauthcode(String phone) {
        params.clear();
        params.put("phone", phone);
        params.put("type", "sms");
        params.put("function", "forget_pwd");
        getDate(params, Constants.CAPTCHA, handler, SENDVCODEMSGSUCCES, SENDVCODEMSGFAIL, true,
            false, false);
    }

    private void checkVcode(String phoneStr, String codeStr) {
        params.clear();
        params.put("phone", phoneStr);
        params.put("code", codeStr);
        getDate(params, Constants.VERIFYCAP, handler, CHECKVCODEMSGSUCCES, CHECKVCODEMSGFAIL,
            false, false, false);

    }

    private void regist(String psw) {
        params.clear();
        params.put("token", registToken);
        params.put("password", psw);
        params.put("deviceId", Tools.getDeviceId(mContext));
        if (invitation_edit.getText().toString().trim() != null) {
            params.put("inviteCode", invitation_edit.getText().toString().trim());
        }
        if (JPushInterface.getRegistrationID(LoginPage.this) != null) {
            params.put("jpushId", JPushInterface.getRegistrationID(LoginPage.this));
        }
        params.put("from", PackageUtils.getAppMetaData(LoginPage.this, "UMENG_CHANNEL"));
        postDate(params, Constants.REGIST, handler, REGISTSUCCES, REGISTFAIL, true, false, false);
    }

    private void getVcode(String phone, String type) {
        params.clear();
        params.put("phone", phone);
        params.put("type", type);

        getDate(params, Constants.CAPTCHAREGIST, handler, SENDVCODESUCCES, SENDVCODEFAIL, true,
            false, false);
    }

    private void login(String mobile, String psd) {
        params.clear();
        params.put("phone", mobile);
        params.put("password", psd);
        params.put("deviceId", Tools.getDeviceId(getContext()));
        if (JPushInterface.getRegistrationID(LoginPage.this) != null) {
            params.put("jpushId", JPushInterface.getRegistrationID(LoginPage.this));
        }
        postDate(params, Constants.LOGIN, handler, LOGINSUCCES, LOGINFAIL, true, false, false);
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case LOGINFAIL:
                    showToast("登录失败");
                    break;
                case LOGINSUCCES:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        User user = JSONObject.parseObject(result.data, User.class);
                        application.setUser(user);
                        Intent intent = new Intent();
                        intent.setClass(getContext(), SetGesturePsdPage.class);
                        startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);
                    } else {
                        showToast(result.codeDesc);
                    }
                    break;

                //注册界面相关
                case SENDVCODESUCCES:
                    Basebean result_1 = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_1.isOk()) {
                        if ("sms".equals(verify_type)) {
                            showToast("短信验证码已发送");
                            countDownType = "regist_cd";
                            timer_show(regist_verification_bt);
                        } else if ("voice".equals(verify_type)) {
                            RegistVoiceBean voiceVcode = JSONObject.parseObject(result_1.data,
                                RegistVoiceBean.class);

                            alertDialog = PromptManager.showConfirmAlertCommon(mContext,
                                0, "", "请等待10s\n" + voiceVcode.getVoicePhone()
                                                       + "将为您播报语音验证码", "确定", "", false,
                                new ReChargeClickListener() {

                                    @Override
                                    public void onClickConfirm() {

                                    }

                                    @Override
                                    public void onClickCancel() {

                                    }
                                });

                            new Thread(new Runnable() {
                                public void run() {
                                    SystemClock.sleep(5000);
                                    handler.sendEmptyMessage(DISMISSDIAOLOG);
                                }
                            }) {
                            }.start();
                        }
                    } else {
                        showToast(result_1.codeDesc);
                    }
                    break;
                case SENDVCODEFAIL:

                    break;
                case CHECKVCODESUSSES:
                    Basebean result_v = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_v.isOk()) {
                        Register register = JSONObject.parseObject(result_v.data, Register.class);
                        registToken = register.token;
                        ActivitySwitcher.animationOutForward(regist_layout, getWindowManager(),
                            new ActivitySwitcher.AnimationFinishedListener() {
                                @Override
                                public void onAnimationFinished() {
                                    regist_layout.setVisibility(View.GONE);
                                    registnext_layout.setVisibility(View.VISIBLE);
                                    ActivitySwitcher.animationInForward(registnext_layout,
                                        getWindowManager());
                                }
                            });

                    } else {
                        showToast(result_v.codeDesc);
                    }

                    break;
                case CHECKVCODEFAIL:
                    break;
                case DISMISSDIAOLOG:
                    if (alertDialog != null && alertDialog.isShowing()) {
                        alertDialog.dismiss();
                    }
                    ;
                    break;

                //registernext 
                case REGISTSUCCES:
                    Basebean reslut = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (reslut.isOk()) {
                        User user = JSONObject.parseObject(reslut.data, User.class);
                        application.setRegistSuccess(true); //置为true，返回registerpage界面就会消失
                        application.setUser(user);
                        Intent intent = new Intent(getContext(), SetGesturePsdPage.class);
                        intent.putExtra("formregist", true);
                        startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);
                    } else {
                        application.setRegistSuccess(false); //置为false，返回registerpage界面不会消失
                        showToast(reslut.codeDesc);
                    }
                    break;
                case REGISTFAIL:
                    application.setRegistSuccess(false); //置为false，返回registerpage界面不会消失
                    showToast("网络异常，请重试");
                    break;

                //findpwd

                case SENDVCODEMSGSUCCES:
                    Basebean responseInfo = JSONObject
                        .parseObject((String) msg.obj, Basebean.class);
                    if (responseInfo.isOk()) {
                        showToast("短信验证码已发送");
                        countDownType = "password_cd";
                        timer_show(code_btn);
                    } else {
                        showToast(responseInfo.codeDesc);
                    }
                    break;
                case SENDVCODEMSGFAIL:
                    showToast("网络异常");
                    break;
                case CHECKVCODEMSGSUCCES:
                    Basebean result_v2 = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_v2.isOk()) {
                        CheckVcode responseCheckVcodeInfo = JSONObject.parseObject(result_v2.data,
                            CheckVcode.class);
                        findPwdNextToken = responseCheckVcodeInfo.getToken();

                        ActivitySwitcher.animationOutForward(findpwd_layout, getWindowManager(),
                            new ActivitySwitcher.AnimationFinishedListener() {
                                @Override
                                public void onAnimationFinished() {
                                    findpwd_layout.setVisibility(View.GONE);
                                    findpwdnext_layout.setVisibility(View.VISIBLE);
                                    ActivitySwitcher.animationInForward(findpwdnext_layout,
                                        getWindowManager());
                                }
                            });
                    } else {
                        showToast(result_v2.codeDesc);
                    }
                    break;
                case TIMECOUNTMESSAGE:

                    if("password_cd".equals(countDownType)){
                        code_btn.setEnabled(true);
                        code_btn.setText(btnMsg);
                    }else if("regist_cd".equals(countDownType)){
                        regist_verification_bt.setEnabled(true);
                        regist_verification_bt.setText(btnMsg);
                    }
                    // timer 取消执行
                    timer.cancel();
                    break;
                case CHECKVCODEMSGFAIL:
                    showToast("网络异常");
                    break;

                case MODIFYPSWSUCCES:
                    Basebean result_pw = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_pw.isOk()) {
                        showToast("修改密码成功");
                        ActivitySwitcher.animationOutBack(findpwdnext_layout, getWindowManager(), new AnimationFinishedListener() {
                            
                            @Override
                            public void onAnimationFinished() {
                                findpwdnext_layout.setVisibility(View.GONE);
                                login_layout.setVisibility(View.VISIBLE);
                                ActivitySwitcher.animationInBack(login_layout, getWindowManager());
                            }
                        });
                    } else {
                        showToast(result_pw.codeDesc);
                    }
                    break;
                case MODIFYPSWFAIL:
                    showToast("修改失败");
                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
        iv_finish.setOnClickListener(this);
        iv_finish2.setOnClickListener(this);
        iv_finish3.setOnClickListener(this);
        iv_finish4.setOnClickListener(this);
        find_password_bt.setOnClickListener(this);
        login_bt.setOnClickListener(this);
        rigst_bt.setOnClickListener(this);
        tv_backtologin.setOnClickListener(this);
        tv_backtologin2.setOnClickListener(this);
        tv_backtologin3.setOnClickListener(this);
        tv_backtologin4.setOnClickListener(this);
        regist_verification_bt.setOnClickListener(this);
        regist_button.setOnClickListener(this);
        getvoice_vcode.setOnClickListener(this);
        regist_btn.setOnClickListener(this);
        code_btn.setOnClickListener(this);
        next.setOnClickListener(this);
        iv_finish5.setOnClickListener(this);
        next_btn.setOnClickListener(this);

        psw_isvisable_pwdnext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isShow) {
                    set_psw_et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    psw_isvisable_pwdnext.setBackgroundResource(R.drawable.password_plan_on);
                    isShow = false;
                } else {
                    set_psw_et.setInputType(InputType.TYPE_CLASS_TEXT
                                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    psw_isvisable_pwdnext.setBackgroundResource(R.drawable.password_plan_off);
                    isShow = true;
                }
            }
        });
        set_psw_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString()) || s.toString().length() < 8) {
                    next_btn.setEnabled(false);
                } else {
                    next_btn.setEnabled(true);
                }
            }
        });

        set_login_psw.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim()) || s.toString().length() < 8) {
                    regist_btn.setEnabled(false);
                } else {
                    regist_btn.setEnabled(true);
                }
            }
        });

        psw_isvisable.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    set_login_psw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    set_login_psw.setInputType(InputType.TYPE_CLASS_TEXT
                                               | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });

        regist_mobile_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s) || s.length() != 11 || !StringUtils.isEmpty(mobile)) {
                    regist_button.setEnabled(false);
                } else {
                    regist_button.setEnabled(true);
                }

            }
        });
        regist_verification_ev.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    regist_button.setEnabled(false);
                } else {
                    regist_button.setEnabled(true);
                }

            }
        });

        phone_code.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s) || s.length() < 1
                    || StringUtils.isEmpty(phone.getText().toString().trim())) {
                    next.setEnabled(false);
                } else {
                    next.setEnabled(true);
                }
            }
        });

        login_mobile_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) || s.length() == 11) {
                    login_bt.setEnabled(true);
                } else {
                    login_bt.setEnabled(false);
                }

            }
        });
        login_psd_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    login_bt.setEnabled(false);
                } else {
                    login_bt.setEnabled(true);
                }
            }
        });
    }

    @SuppressLint("NewApi")
    private void timer_show(final Button view) {

        if(timer != null){
            timer.cancel();
        }

        btnMsg = view.getText().toString();
        if("password_cd".equals(countDownType)){
            regist_verification_bt.setEnabled(true);
            regist_verification_bt.setText(btnMsg);

        }else if("regist_cd".equals(countDownType)){

            code_btn.setEnabled(true);
            code_btn.setText(btnMsg);
        }
        time = 60;
        // 设置按钮不可点击
        view.setEnabled(false);
        timer = new Timer();
        // 点击1毫秒开始执行，没个一秒执行一次
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                int i = getInt();
                view.post(new ButtonTextFresh(i, view));
                // 若倒计时到0时
                if (i == 0) {
                    handler.sendEmptyMessage(TIMECOUNTMESSAGE);
                }
            }
        }, 1, 1000);
    }

    /**
     * 获取倒计时的计数
     * 
     * @return
     */
    private int getInt() {
        return time--;
    }

    class ButtonTextFresh implements Runnable {

        private int iMsg;
        private Button view;

        public ButtonTextFresh(int iMsg, Button view) {
            this.iMsg = iMsg;
            this.view = view;
        }

        @Override
        public void run() {
            view.setText(btnMsg + "(" + iMsg + ")");
        }
    }

    @Override
    protected void processLogic() {
        initPage();
    }

    private void initPage() {
        params = getParm();
        subTitle.setTitle("登录");
        subTitle.setLeftName("取消");
        subTitle.title_left_tv.setTextColor(getResources().getColor(R.color.gray_1));
        subTitle.title_left_tv.setVisibility(View.VISIBLE);
        
        String witchLayout = getIntent().getStringExtra("witchLayout");
        if ("findpwd".equals(witchLayout)) {
            findpwd_layout.setVisibility(View.VISIBLE);
        } else if ("regist".equals(witchLayout)) {
            regist_layout.setVisibility(View.VISIBLE);
        } else {   
            login_layout.setVisibility(View.VISIBLE);
            application.logout();
            login_mobile_et.setText(application.getUser().phone);
        }
    }

    /**
     * 取消登录
     * 登陆时传递唤起唯一ID
     * @see com.twotiger.and.activity.base.BaseActivity#onBackPress()
     */
    @Override
    protected void onBackPress() {
        int fromId = getIntent().getIntExtra("FROM", 0);
        switch (fromId) {
        case -1:
            startNewActivity(new Intent(this, HomeActivity.class), R.anim.push_left_in,
                R.anim.push_left_out, true);
            break;
        case R.id.radio_btn3:
        case R.id.acount_layout:
        case R.id.exit_acount:
        case R.id.cut_account_bt:
        case R.id.forget_psd_bt:
        case R.id.tv_backtologin:
        case R.id.to_inverst:
            finish();
            overridePendingTransition(R.anim.silent_anim, R.anim.slide_out_to_bottom);
            break;
        default:
            finish();
            overridePendingTransition(R.anim.silent_anim, R.anim.slide_out_to_bottom);
            break;
        }
    }
}
